﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ASOFT.PRINTER
{
    public static class Setting
    {
        private static string path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        private static string NameSpace = "ASOFT.ERP.PRINTER";

        public static void EnableStartOnWindowsStart()
        {
            if (!registryValueExists("HKCU", path, NameSpace))
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true);
                key.SetValue(NameSpace, Application.ExecutablePath.ToString());
            }

        }

        public static void DisableStartOnWindowsStart()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true);
            key.DeleteValue(NameSpace, false);
        }



        public static bool registryValueExists(string hive_HKLM_or_HKCU, string registryRoot, string valueName)
        {
            RegistryKey root;
            switch (hive_HKLM_or_HKCU.ToUpper())
            {
                case "HKLM":
                    root = Registry.LocalMachine.OpenSubKey(registryRoot, false);
                    break;
                case "HKCU":
                    root = Registry.CurrentUser.OpenSubKey(registryRoot, false);
                    break;
                default:
                    throw new System.InvalidOperationException("parameter registryRoot must be either \"HKLM\" or \"HKCU\"");
            }

            if (root.GetValue(valueName) == null)
                return false;
            else
                return true;
        }


    }
}
